import {mapGetters} from 'vuex'
const usally = {
  computed:{
    ...mapGetters('compet',['dealAllPoint'])
  },
  methods: {
    //过滤对象中的空值
    filterObjEmpty(obj) {
      let newObj = {}
      for(let key in obj){
        if(obj[key]){
          newObj[key] = obj[key]
        }
      }
      return newObj
    },
    //比较时间大小
    compareTime(start,end){
      let isTure = false;
      let starttime = start+':00'
      let endtime = end+':00'
      starttime = starttime.substring(0,19);
      starttime = starttime.replace(/-/g,'/');
      let startDatetimestamp = new Date(starttime).getTime();
      endtime = endtime.substring(0,19);
      endtime = endtime.replace(/-/g,'/');
      let endDatestimestamp = new Date(endtime).getTime();
      if(endDatestimestamp<=startDatetimestamp){
        isTure = true;
      }
      return isTure;
    },
    //竞赛过滤器
    statusFilter(val){
      let str = '';
      let color = ''
      switch (val) {
        case '0':
          str = '未开始';
          color = 'var(--status-pre)'
          break;
        case '1':
          str = '进行中';
          color = 'var(--status-ing)'
          break;
        case '2':
          str = '已结束';
          color = 'var(--status-deling)'
          break;
        default:
          break;
      }
      return {txt:str,color};
    },
    //难度过滤
    difficultyFilter(val){
      let difficultyStr='';
      if(val){
        val = val.toLowerCase();
      }
      switch(val){
        case 'simple':difficultyStr = '简单';break;
        case 'commonly':difficultyStr = '一般';break;
        case 'medium':difficultyStr = '中等';break;
        case 'hard':difficultyStr = '较难';break;
      }
      return difficultyStr;
    },
    filterAllPoints(val){ //获取所有方向
      let arr = []
      let splitVal = val.split(',');
      let typeCodeObj = this.dealAllPoint || []
      splitVal.forEach(split=>{
        let curCode = typeCodeObj.filter(code=>code.dictid===split)[0];
        if(curCode){
          arr.push(curCode.dictname)
        }
      })
      if(arr.length===0){
        arr=['未知类型']
      }
      return arr;
    },
    //获取正确答案
    getRightAnswer(val){
      let answer = [];
      val.forEach(item=>{
        if(item.isRight){
          answer.push(item.description || item.explanation)
        }
      })
      return answer.join(';');
    },
    // 获取判断题的正确答案
    getRightAnswerByJudge(val = []) {
      let answer = [];
      val.forEach(item=>{
        answer.push(item.isRight === 1 ? '正确' : '错误')
      })
      return answer.join(';')
    },
    //对题目答案进行排序
    sortQuestionAnswer(answer){
      if(!answer){
        return;
      }else if(answer.length>0&&answer[0].no){
        answer.sort(function(a,b){
          return parseInt(a.no)-parseInt(b.no);
        })
      }
      return answer;
    }
  }
}
export default usally
